<div class="page-header">
	<ul id="table-tab" class="nav nav-tabs" role="tablist">
		<li role="presentation" class="active"><a href="#account-tab"
			aria-controls="account-tab" role="tab" data-toggle="tab">账号管理</a></li>
	</ul>
</div>
<div class="tab-content">
	<div id="account-tab" class="tab-pane active" role="tabpanel">
		<div class="row">
			<div class="col-md-3">
            	<div class="widget-box">
                    <div class="widget-body">
                        <div class="widget-main padding-8" style="overflow-x:auto;">
                            <ul id="treeorg" class="ztree"></ul>
                        </div>
                    </div>
                </div>
            </div>
			<!--搜索域-->
			<div class="col-md-9">
				<div class="search-area">
					<form id="table-account-search" class="form-inline">
						<input type="hidden" id="account-orgid" name="account.orgid" value="">
						<div class="form-group">
							<label for="account.username">账号：</label> <input type="text"
								class="form-control input-sm" id="account-username"
								name="account.username" value="" placeholder="按账号搜索">
						</div>
						<div class="form-group">
							<label for="account.email">邮箱：</label> <input type="text"
								class="form-control input-sm" id="account-email"
								name="account.email" value="" placeholder="按邮箱搜索">
						</div>
						<div class="form-group">
							<label for="account.mobile">手机：</label> <input type="text"
								class="form-control input-sm" id="account-mobile"
								name="account.mobile" value="" placeholder="按手机搜索">
						</div>
						<button id="btn-account-search" type="button"
							class="btn btn-info btn-sm no-border">
							<i class="ace-icon fa fa-search bigger-110"></i>查询
						</button>
						<div class="form-group pull-right">
							<span>查询方式：</span> <label class="radio-inline"> <input
								type="radio" name="searchType" value="1" checked="checked" />精确
							</label> <label class="radio-inline"> <input type="radio"
								name="searchType" value="0" />模糊
							</label>
						</div>
					</form>
				</div>
				<!-- 数据表格 -->
				<div id="table-account-toolbar">
					<div class="btn-group btn-group-sm">
						<!-- 
						<button id="addAccount" class="btn btn-info btn-sm no-border"
							onclick="goAddBefore('');">
							<i class="ace-icon fa fa-plus bigger-110"></i>新增
						</button>
						<button id="editAccount" class="btn btn-success btn-sm no-border"
							onclick="goUpdate('编辑账号', 'system/admin/account/edit', 'table-account');">
							<i class="ace-icon fa fa-pencil-square-o bigger-110"></i>编辑
						</button>
						<button id="delAccount" class="btn btn-danger btn-sm no-border"
							onclick="doDels('system/admin/account/deletes', 'table-account');">
							<i class="ace-icon fa fa-trash-o bigger-110"></i>删除
						</button>
	                    <button id="allocGroup" class="btn btn-success btn-sm no-border"
							onclick="goAlloc('分配用户组', 'system/admin/account/allocGroup');">
							<i class="ace-icon fa fa-pencil-square-o bigger-110"></i>分配用户组
						</button>
						-->
						
						<x:toolbar menuId="${menuId}" elemIds="addAccount,editAccount,delAccount,allocGroup">
                    	</x:toolbar>
					</div>
				</div>
				<table id="table-account" data-toolbar="#table-account-toolbar"
					data-sort-name="uuid" data-sort-order="asc"
					data-show-columns="true" data-id-field="uuid">
					<thead>
						<tr>
							<th data-field="state" data-checkbox="true"></th>
							<th data-width="120" data-field="username" data-align="left"
								data-sortable="false">账号</th>
							<th data-width="100" data-field="email" data-align="left"
								data-sortable="true">邮箱</th>
							<th data-width="200" data-field="mobile" data-align="left"
								data-sortable="true">手机</th>
							<th data-width="200" data-field="locked" data-align="left"
								data-formatter="formatLocked">锁定状态</th>
							<th data-field="userid" data-align="left"
								data-formatter="formatUser">资料</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
	function lock(uuid) {
		$.ajax({
			url : 'system/admin/account/lock',
			type : 'post',
			data : {
				id : uuid
			},
			success : function(resp) {
				if (resp.status == "OK") {
					$("#table-account").bootstrapTable('refresh');
					TipMsg.success(resp.message, 0);
				} else {
					TipMsg.error(resp.message, 0);
				}
			},
			error : function(resp) {
				TipMsg.error("发生错误!", 0);
			}
		});
	}

	function unlock(uuid) {
		$.ajax({
			url : 'system/admin/account/unlock',
			type : 'post',
			data : {
				id : uuid
			},
			success : function(resp) {
				if (resp.status == "OK") {
					$("#table-account").bootstrapTable('refresh');
					TipMsg.success(resp.message, 0);
				} else {
					TipMsg.error(resp.message, 0);
				}
			},
			error : function(resp) {
				TipMsg.error("发生错误!", 0);
			}
		});
	}

	function formatLocked(value, row, index) {
		var val = parseInt(value)
		switch (val) {
		case 0:
			return "<a href=\"javascript:lock('"
					+ row.uuid
					+ "')\"><i class=\"ace-icon fa fa-unlock bigger-110\"></i></a>";
		case 1:
			return "<a href=\"javascript:unlock('"
					+ row.uuid
					+ "')\"><i class=\"ace-icon fa fa-lock bigger-110\"></i></a>";
		default:
			return value;
		}
	}
	
	function editUser(userid) {
		var title = "用户资料";
		var url = "system/admin/account/editUser?userid=" + userid;
		loadModal(url, function(data, status) {
			if (status === "success") {
				changeModalTitle(title);
				showModal();
			} else if (status === "timeout") {
				TipMsg.error("请求超时，请重试!!!", 0);
			}
		});
	};

	function formatUser(value, row, index) {
		return "<a href=\"javascript:editUser('" + value + "')\"><i class=\"ace-icon fa fa-edit bigger-110\"></i></a>"
	}
	
	function goAlloc(title, url) {
		var selections = $('#table-account').bootstrapTable("getSelections");
		if (selections !== undefined && selections !== null) {
			if (selections.length === 1) {
				var id = selections[0].uuid;
				url = url + '?accountId=' + id;
				loadModal(url, function(data, status) {
					if (status === "success") {
						changeModalTitle(title);
						showModal();
					} else if (status === "timeout") {
						TipMsg.error("请求超时，请重试!!!", 0);
					}
				});
			} else {
				TipMsg.error("请选择一个用户.", 0);
			}
		} else {
			TipMsg.error("没有可分配的用户.", 0);
		}
	};
	
	$(document).ready(function() {
		
		$.fn.zTree.init($("#treeorg"), {
			async: {
				enable: true,
				url:"system/admin/org/tree",
				autoParam:["uuid", "alevel"],
				dataFilter: ajaxFilter
			},
			callback:{
				onClick:treeOnClick
			},
			data: {
				simpleData: {
					enable: true,
					idKey: "uuid",
					pIdKey: "parentid",
					rootPId: null
				}
			},
			check: {
				enable: false,
				autoCheckTrigger: false,
				chkboxType: { "Y" : "p", "N" : "p" }
			}
		});

		function ajaxFilter(treeId, pNode, respData){
			var nodes = null;
			if(respData.status == 'OK'){
				nodes = respData.rows;
			}
			if(nodes != null && nodes.length > 0){
				$('#account-orgid').val(nodes[0].uuid);
				$('#addaccount').attr("onclick", "goAddBefore('" + nodes[0].uuid + "');");
				initTableList('table-account', 'system/admin/account/dataGrid', 'table-account-search', 'btn-account-search');
			}
			return nodes;
		}
		
		function treeOnClick(event, treeId, treeNode){
			if(treeNode.uuid == null){
				$('#account-orgid').val("");
			} else {
				$('#account-orgid').val(treeNode.uuid);
			}
			$('#table-account').bootstrapTable('selectPage', 1);
			//刷新完毕还原
			//$('#org-level').val("");
			//$('#org-parentid').val("");
			//改变add 参数
			var uuid = '';
			if(treeNode.uuid !== null && treeNode.uuid != undefined){
				uuid = treeNode.uuid;
			}
			$('#addAccount').attr("onclick", "goAddBefore('" + uuid + "');");
		};
	});
	
	function goAddBefore(uuid){
		if(uuid == '' || uuid == 'undefined'){
			TipMsg.error("请选择一个有效的机构进行新增操作", 0);
		} else {
			var url = 'system/admin/account/add?orgid=' + uuid;
			goAdd('新增账号', url, 'table-account');
		}
	}
</script>
